package com.lg.concurrent.aqs_cas;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MyLockTest {
    public static void main(String[] args) {
        MyLock myLock = new MyLock();
        int num = 4;
        Thread [] thread = new Thread[num];
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            thread[i] = new Thread(() -> {
                myLock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + " 进入锁并开始使用");
                    TimeUnit.MINUTES.sleep(random.nextInt(num));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    System.out.println(Thread.currentThread().getName() + " 使用锁完毕");
                    myLock.unlock();
                }
            });
        }

        for (int i = 0; i < num; i++) {
            thread[i].start();
        }
        myLock.unlock();
    }
}
